package us.icitap.cm.ui.policecase;

import java.util.Arrays;

import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.Evidence;
import us.icitap.cm.business.entities.Evidence.EvidenceTypeEnum;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustDateField;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.utils.ProjectLists;

import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.VerticalLayout;

public class EvidenceEmbeddableDataLayout extends VerticalLayout{

	private Evidence evidence;
	
	private final CustTextField evidenceNoTextField = new CustTextField("Nr. i proves (sipas listes se inventarit)");
	private final CustDateField evidenceDateDateField = new CustDateField("Data e marrjes/gjetjes se proves");
	private final CustComboBox evidenceTypeSelect = new CustComboBox("Lloji i proves");
	private final CustComboBox evidenceIdentificationSelect = new CustComboBox("Menyra e marrjes/gjetjes");
	
	private final CustTextArea notesTextArea = new CustTextArea("Shenime mbi proven");
	
	private final CustBeanFieldGroup<Evidence> binder = new CustBeanFieldGroup<Evidence>(Evidence.class);
	

	public EvidenceEmbeddableDataLayout() {
		this.setMargin(true);
		
		FormLayout fl = new FormLayout();
		fl.addComponent(this.evidenceNoTextField);
		fl.addComponent(this.evidenceDateDateField);
		fl.addComponent(this.evidenceTypeSelect);
		fl.addComponent(this.evidenceIdentificationSelect);
		fl.addComponent(this.notesTextArea);

		this.addComponent(fl);
		
		binder.bind(evidenceNoTextField, "evidenceNo");
		binder.bind(evidenceDateDateField, "evidenceDate");
		binder.bind(evidenceTypeSelect, "evidenceType");
		binder.bind(evidenceIdentificationSelect, "evidenceIdentMethod");
		binder.bind(notesTextArea, "notes");
		
		
		evidenceTypeSelect.setContainerDataSource(new BeanItemContainer<EvidenceTypeEnum>(EvidenceTypeEnum.class, Arrays.asList(EvidenceTypeEnum.values())));

		evidenceIdentificationSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.EvidenceIdentMethod, false)));
		
		
		
		
		
		
		
	}
	

	
	public void loadEvidence(Evidence evidence, boolean editable){

		this.evidence = evidence;
		binder.setItemDataSource(evidence);
		binder.setReadOnly(!editable);
		
		
	}
	
	public void createEvidence(EvidenceTypeEnum evidenceTypeEnum){
		evidence  = new Evidence();
		evidence.setEvidenceType(evidenceTypeEnum);
		binder.setItemDataSource(evidence);
		
		
	}
	
	public Evidence getEvidence() throws CommitException{
		binder.commit();
		
		return evidence;
		
	}

	
}
